﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMSSoft.Lib.Basic;
using WMSSoft.Lib.Enums;
using WMSSoft.Lib.WinKStart;
using WMSSoft.Lib.Texte;
using WMSSoft.Lib.WinKStart.Model;
using WMSSoft.Lib.WinKStart.ViewModel;
using WMSSoft.Lib.WinKStart.Extension;
using WMSSoft.Lib.WinKStart.Abstract;
using WMSSoft.Lib.WinKStart.Model.MenuConfig;
using WMSSoft.Lib.WinKStart.Classes.Metadata;
using System.Reflection;

namespace WMSSoft.WinKStart.ViewModel
{
    public class Setting_NewEditRegionViewModel : SettingBaseListViewModel
    {

        public event EventHandler DataChanged;
        cRegion _Data = null;
        WMSSoft.Lib.WinKStart.Model.MenuConfig.MinimizeablePanel _SelectedPanelItem = null;
        List<WMSSoft.Lib.WinKStart.Model.MenuConfig.MinimizeablePanel> _Panels = null;
        List<KeyValueAssignment> _RegionTypeItems = null;
        List<KeyValueAssignment> _DataSourceItems = null;
        List<KeyValueAssignment> _NavigationStates = null;
        List<KeyValueAssignment> _ContextMenuStates = null;
        string _RegionName = "";
        string _RegionCaption = "";
        string _RegionType = "";
        string _RegionDataSource = "";
        string _RegionFilter = "";
        string _NavigationState = "false";
        string _UseContextMenu = "false";
        bool _CanEditName = true;
        bool _PanelSelected = false;
        bool _CanEditDeleteSelectedPanelItem = false;
        bool _CanChooseFilter = false;
        bool _DataChanged = false;

        #region Initialize
        public Setting_NewEditRegionViewModel()
        {
            base.ViewName = LanguageStrings.GetText("New Region");
            PropertyChanged += Setting_NewEditRegionViewModel_PropertyChanged;
            _Data = new cRegion();
            Panels = new List<WMSSoft.Lib.WinKStart.Model.MenuConfig.MinimizeablePanel>();

            _RegionTypeItems = Meta_Regions.GetRegionTypes();
            _DataSourceItems = Meta_Regions.GetDataSources();
            _NavigationStates = Meta_Regions.GetNavigationStates();
            _ContextMenuStates = Meta_Regions.GetContextMenuStates();

            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("..."), new CDelegatingCommand(Delegate => this.Cmd_Exec_ChooseFilter())));

            ViewModel_OkClick += Setting_NewEditRegionViewModel_ViewModel_OkClick;
            ViewModel_CloseClick += Setting_NewEditRegionViewModel_ViewModel_CloseClick;
            ViewModel_ApplyClick += Setting_NewEditRegionViewModel_ViewModel_ApplyClick;
            ViewModel_AddClick += Setting_NewEditRegionViewModel_ViewModel_AddClick;
            ViewModel_EditClick += Setting_NewEditRegionViewModel_ViewModel_EditClick;
            ViewModel_DeleteClick += Setting_NewEditRegionViewModel_ViewModel_DeleteClick;

            //CanEditDeleteSelectedPanelItem = false;
        }
        
        public Setting_NewEditRegionViewModel(cRegion Data)
        {
            base.ViewName = LanguageStrings.GetText("Edit Region");
            PropertyChanged += Setting_NewEditRegionViewModel_PropertyChanged;

            CanEditName = false;

            _RegionTypeItems = Meta_Regions.GetRegionTypes();
            _DataSourceItems = Meta_Regions.GetDataSources();
            _NavigationStates = Meta_Regions.GetNavigationStates();
            _ContextMenuStates = Meta_Regions.GetContextMenuStates();

            _Data = Data;
            RegionName = _Data.Name;
            RegionCaption = _Data.Caption;
            RegionType = _Data.RendererMethode.ToString();
            RegionDataSource = _Data.DataSource;
            RegionFilter = _Data.Filter;
            NavigationState = _Data.CanNavigate.ToString().ToLower();
            UseContextMenu = _Data.ShowContextMenu.ToString().ToLower();
            Panels = _Data.Panels.Panels;

            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("..."), new CDelegatingCommand(Delegate => this.Cmd_Exec_ChooseFilter())));

            ViewModel_OkClick += Setting_NewEditRegionViewModel_ViewModel_OkClick;
            ViewModel_CloseClick += Setting_NewEditRegionViewModel_ViewModel_CloseClick;
            ViewModel_ApplyClick += Setting_NewEditRegionViewModel_ViewModel_ApplyClick;
            ViewModel_AddClick += Setting_NewEditRegionViewModel_ViewModel_AddClick;
            ViewModel_EditClick += Setting_NewEditRegionViewModel_ViewModel_EditClick;
            ViewModel_DeleteClick += Setting_NewEditRegionViewModel_ViewModel_DeleteClick;

            _DataChanged = false;

            //CanEditDeleteSelectedPanelItem = false;
        }
                      
        #endregion

        #region Properties

        public CSingleCommandViewModel Cmd_ChooseFilter
        {
            get { return m_oCommands[6]; }
        }

        public cRegion Data
        {
            get { return _Data; }
            set
            {
                _Data = value;
                OnPropertyChanged("Data");
            }
        }

        public List<KeyValueAssignment> RegionTypeItems
        {
            get { return _RegionTypeItems; }
            set
            {
                _RegionTypeItems = value;
                OnPropertyChanged("RegionTypeItems");
            }
        }

        public List<KeyValueAssignment> DataSourceItems
        {
            get { return _DataSourceItems; }
            set
            {
                _DataSourceItems = value;
                OnPropertyChanged("DataSourceItems");
            }
        }

        public string RegionName
        {
            get { return _RegionName; }
            set
            {
                _RegionName = value;
                OnPropertyChanged("RegionName");
            }
        }

        public string RegionCaption
        {
            get { return _RegionCaption; }
            set
            {
                _RegionCaption = value;
                OnPropertyChanged("RegionCaption");
            }
        }

        public string RegionType
        {
            get { return _RegionType; }
            set
            {
                _RegionType = value;
                OnPropertyChanged("RegionType");
            }
        }

        public string RegionDataSource
        {
            get { return _RegionDataSource; }
            set
            {
                _RegionDataSource = value;
                OnPropertyChanged("RegionDataSource");
            }
        }

        public string RegionFilter
        {
            get { return _RegionFilter; }
            set
            {
                _RegionFilter = value;
                OnPropertyChanged("RegionFilter");
            }
        }

        public string NavigationState
        {
            get { return _NavigationState; }
            set
            {
                _NavigationState = value;
                OnPropertyChanged("NavigationState");
            }
        }

        public List<KeyValueAssignment> NavigationStates
        {
            get { return _NavigationStates; }
            set
            {
                _NavigationStates = value;
                OnPropertyChanged("NavigationStates");
            }
        }

        public List<KeyValueAssignment> ContextMenuStates
        {
            get { return _ContextMenuStates; }
            set
            {
                _ContextMenuStates = value;
                OnPropertyChanged("ContextMenuStates");
            }
        }

        public string UseContextMenu
        {
            get { return _UseContextMenu; }
            set
            {
                _UseContextMenu = value;
                OnPropertyChanged("UseContextMenu");
            }
        }

        public bool PanelSelected
        {
            get { return _PanelSelected; }
            set
            {
                _PanelSelected = value;
                OnPropertyChanged("PanelSelected");
            }
        }

        public List<WMSSoft.Lib.WinKStart.Model.MenuConfig.MinimizeablePanel> Panels
        {
            get { return _Panels; }
            set
            {
                _Panels = value;
                OnPropertyChanged("Panels");
            }
        }

        public WMSSoft.Lib.WinKStart.Model.MenuConfig.MinimizeablePanel SelectedPanelItem
        {
            get { return _SelectedPanelItem; }
            set
            {
                _SelectedPanelItem = value;
                OnPropertyChanged("SelectedPanelItem");
            }
        }

        public bool CanEditName
        {
            get { return _CanEditName; }
            set
            {
                _CanEditName = value;
                OnPropertyChanged("CanEditName");
            }
        }

        public bool CanEditDeleteSelectedPanelItem
        {
            get { return _CanEditDeleteSelectedPanelItem; }
            set
            {
                _CanEditDeleteSelectedPanelItem = value;
                OnPropertyChanged("CanEditDeleteSelectedPanelItem");
            }
        }

        public bool CanChooseFilter
        {
            get { return _CanChooseFilter; }
            set
            {
                _CanChooseFilter = value;
                OnPropertyChanged("CanChooseFilter");
            }
        }
        //

        #endregion

        #region Command Procedures

        void Setting_NewEditRegionViewModel_ViewModel_OkClick(object sender, EventArgs e)
        {
            try
            {
                Settings_NewEditRegion parentDlg = null;
                if ((Settings_NewEditRegion)((CDelegatingCommand)Cmd_OkClick.Command).CommandParameter != null)
                {
                    parentDlg = (Settings_NewEditRegion)((CDelegatingCommand)Cmd_OkClick.Command).CommandParameter;

                    if (parentDlg != null)
                    {
                        if (ValidateInputs() == true)
                        {
                            parentDlg.DialogResult = true;
                            parentDlg.Close();
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText("Error on applying Data!"), Ex, EMessageTypes.Error);
            } 
        }

        void Setting_NewEditRegionViewModel_ViewModel_CloseClick(object sender, EventArgs e)
        {
            try
            {
                Settings_NewEditRegion parentDlg = null;
                if ((Settings_NewEditRegion)((CDelegatingCommand)Cmd_CloseClick.Command).CommandParameter != null)
                {
                    parentDlg = (Settings_NewEditRegion)((CDelegatingCommand)Cmd_CloseClick.Command).CommandParameter;

                    if (_DataChanged == true)
                    {
                        if (System.Windows.MessageBox.Show(QuestionMessages.QUESTION_CLOSE_DIALOG__LOSE_CHANGES, "Question", 
                            System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.No)
                            return;
                    }

                    if (parentDlg != null)
                        parentDlg.Close();
                    
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText("Can't Close Dialog!"), Ex, EMessageTypes.Error);
            } 
        }

        void Setting_NewEditRegionViewModel_ViewModel_ApplyClick(object sender, EventArgs e)
        {
            try
            {
                //fire event to apply data
                if (ValidateInputs() == true)  if (DataChanged != null) DataChanged(this, EventArgs.Empty);
                _DataChanged = false;
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error on applying Data!", Ex, true, EMessageTypes.Error);
            }
        }

        bool ValidateInputs()
        {
            if (RegionName.Length == 0)
            {
                System.Windows.MessageBox.Show(LanguageStrings.GetText(InfoMessages.INFO_NEW_EDIT_REGION__INVALID_REGION_NAME, EStringTypes.stMessage),
                                                   LanguageStrings.GetText("Info"), System.Windows.MessageBoxButton.OK);
                return false;
            }

            if (RegionName.Length == 0)
            {
                System.Windows.MessageBox.Show(LanguageStrings.GetText(InfoMessages.INFO_NEW_EDIT_REGION__INVALID_REGION_CAPTION, EStringTypes.stMessage),
                                                   LanguageStrings.GetText("Info"), System.Windows.MessageBoxButton.OK);
                return false;
            }

            if (RegionType.Length == 0)
            {
                System.Windows.MessageBox.Show(LanguageStrings.GetText(InfoMessages.INFO_NEW_EDIT_REGION__INVALID_REGION_TYPE, EStringTypes.stMessage),
                                                   LanguageStrings.GetText("Info"), System.Windows.MessageBoxButton.OK);
                return false;
            }

            _Data.Name = RegionName;
            _Data.Caption = RegionCaption;
            _Data.RendererMethode = (eRenderType)Enum.Parse(typeof(eRenderType), RegionType);
            _Data.DataSource = RegionDataSource;
            _Data.Filter = RegionFilter;
            _Data.CanNavigate = bool.Parse(NavigationState);
            _Data.ShowContextMenu = bool.Parse(UseContextMenu);

            _Data.Panels.Panels.Clear();
            foreach (WMSSoft.Lib.WinKStart.Model.MenuConfig.MinimizeablePanel Item in Panels)
            {
                _Data.Panels.Panels.Add(Item);
            }

            return true;
        }

        void Setting_NewEditRegionViewModel_ViewModel_AddClick(object sender, EventArgs e)
        {
            try
            {
                System.Windows.Controls.ListView tmpListBox = null;
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_AddClick.Command).CommandParameter != null)
                {
                    tmpListBox = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_AddClick.Command).CommandParameter;

                    Setting_NewEditPanelViewModel tmpViewModel = new Setting_NewEditPanelViewModel();
                    Setting_NewEditPanel tmpDlg = new Setting_NewEditPanel();
                    tmpDlg.DataContext = tmpViewModel;
                    if (tmpDlg.ShowDialog() == true)
                    {
                        WMSSoft.Lib.WinKStart.Model.MenuConfig.MinimizeablePanel tmpPanel = ((Setting_NewEditPanelViewModel)tmpDlg.DataContext).Panel;
                        if (tmpPanel != null)
                        {
                            Panels.Add(tmpPanel);
                            OnPropertyChanged("Panels");
                            if (tmpListBox != null) tmpListBox.Items.Refresh();
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error on Create new Panel!", Ex, true, EMessageTypes.Error);
            }
        }

        void Setting_NewEditRegionViewModel_ViewModel_EditClick(object sender, EventArgs e)
        {
            try
            {
                System.Windows.Controls.ListView tmpListBox = null;
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_EditClick.Command).CommandParameter != null)
                {
                    tmpListBox = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_EditClick.Command).CommandParameter;
                    if (SelectedPanelItem != null)
                    {
                        Setting_NewEditPanelViewModel tmpViewModel = new Setting_NewEditPanelViewModel(SelectedPanelItem);
                        Setting_NewEditPanel tmpDlg = new Setting_NewEditPanel();
                        tmpDlg.DataContext = tmpViewModel;
                        if (tmpDlg.ShowDialog() == true)
                        {
                            SelectedPanelItem = ((Setting_NewEditPanelViewModel)tmpDlg.DataContext).Panel;
                            tmpListBox.Items.Refresh();
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error on editing Panel!", Ex, true, EMessageTypes.Error);
            }
        }

        void Setting_NewEditRegionViewModel_ViewModel_DeleteClick(object sender, EventArgs e)
        {
            try
            {
                System.Windows.Controls.ListView tmpListBox = null;
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_DeleteClick.Command).CommandParameter != null)
                {
                    tmpListBox = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_DeleteClick.Command).CommandParameter;
                    if (SelectedPanelItem != null)
                    {
                        string sMessageText = QuestionMessages.QUESTION_NEW_EDIT_REGION__DELETE_PANEL;
                        sMessageText = LanguageStrings.GetText(sMessageText);
                        sMessageText = sMessageText.Replace("###PANEL###", SelectedPanelItem.Caption);
                        if (System.Windows.MessageBox.Show(sMessageText, LanguageStrings.GetText("Question"), 
                            System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes)
                        {
                            Panels.Remove(SelectedPanelItem);
                            tmpListBox.Items.Refresh();
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error on deleting Panel!", Ex, true, EMessageTypes.Error);
            }              
        }

        void Cmd_Exec_ChooseFilter() 
        {
            try
            {
                ChooseFilterDlgViewModel tmpChooseDlgViewModel = new ChooseFilterDlgViewModel(_RegionDataSource, _RegionFilter);
                ChooseFilterDlg tmpChooseFilterDlg = new ChooseFilterDlg();
                tmpChooseFilterDlg.DataContext = tmpChooseDlgViewModel;
                if (tmpChooseFilterDlg.ShowDialog() == true)
                {
                    RegionFilter = ((ChooseFilterDlgViewModel)tmpChooseFilterDlg.DataContext).ActiveFilter;
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error while choosing Filter!", Ex, true, EMessageTypes.Error);
            }
        }

        #endregion
        
        void Setting_NewEditRegionViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            try
            {
                _DataChanged = true;
                if (e.PropertyName == "SelectedPanelItem")
                {
                    if (_SelectedPanelItem != null)
                        CanEditDeleteSelectedPanelItem = true;
                    else
                        CanEditDeleteSelectedPanelItem = false;
                } else if (e.PropertyName == "RegionDataSource") {
                    if (_RegionDataSource.Length > 0)
                        CanChooseFilter = true;
                    else
                        CanChooseFilter = false;
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error on Change Property '" + e.PropertyName + "'!", Ex, EMessageTypes.Error);
            }
        }
    }
}
